#!/usr/bin/python
# -*- coding: utf-8 -*-

from google.appengine.ext import db
from google.appengine.api import users
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect, HttpResponse
from django.template.context import RequestContext


def index(request):
    return HttpResponse('Hello, here is net4 Contact Online!!!')
    
def visit(request):
    total = float(VisitRecord.all().count())
    other_rate = 100
    browsers = []
    browsers_rates = []
    for b in VisitRecord.BROWSER_NAMES:
        count = VisitRecord.gql('WHERE browser = :1', b).count()
        if count > 0:
            rate = count/total * 100
            other_rate -= rate
            browsers.append(b)
            browsers_rates.append(rate)
    browsers.append('Others')
    browsers_rates.append(other_rate)
    
    other_rate = 100
    os_names = []
    os_rates = []
    for name in VisitRecord.OS_NAMES:
        count = VisitRecord.gql('WHERE os = :1', name).count()
        if count > 0:
            rate = count/total * 100
            other_rate -= rate
            os_names.append(name)
            os_rates.append(rate)
    os_names.append('Others')
    os_rates.append(other_rate)
    records = VisitRecord.gql('ORDER BY date DESC LIMIT 20')
    return render_to_response('visitrecords.html', 
                              {'records': records,
                               'browsers': browsers,
                               'browsers_rates': browsers_rates,
                               'os_names': os_names,
                               'os_rates': os_rates,
                               },
                              context_instance=RequestContext(request))
    
    
